<template>
	<view class="box">
		<!-- 选择会员 -->
		<view class="selectMember">
			<view class="member">
				<text style="position: relative;">{{start_date || '日期：'}}
					<picker mode="date" :start="start" :end="end" v-if="!selected"  @change="bindDateChange" style="position: absolute;left:0;top: 0;width: 100%;height: 100%;">
						<!-- <view class="date">{{start_date}}</view> -->
					</picker> 
				</text>
			</view>
			<view class="save" @click="showModalbtn(4)" v-if="!selected">保存</view>
			<view class="toSend" @click="showModalbtn(2)" >申请评估</view>
		</view>
		
		<view class="screenModular">
			<view class="specific">
				<image src="@/static/img/healthStatus/screen.png" class="title"></image>
				<view class="single" v-for="(item,index) in questionData[0] && questionData[0].adTopicsRepliesEntityList" :key="index">
					<image src="@/static/img/healthStatus/questionIcon@2x.png"></image>
					<view>
						<view class="question">{{item.name}}</view>
						<view class="answer">{{rapidScreen && rapidScreen[index].setvalue}}</view>
					</view>
				</view>
			</view>	
		</view>
		
		<view class="queationBox">
			<view>问卷详情</view>
			<view>在过去的两周内，有多少时候您受到以下问题困扰</view>
		</view>
		
		<view class="contentDifferentiation">
			<view class="question" v-for="(item,index) in questionData[1] && questionData[1].adTopicsRepliesEntityList" :key="index">
				<view class="title">
					<text>{{index < 9 ?'0'+Number(index+1) : index}}</text>
					<view>{{item.name}}</view>
				</view>
				<view class="option" v-for="(aitem,aindex) in item.adTopicsRepliesEntityList" :key="aindex">
					<view @click="answer(item,aitem)">
						<text>{{aitem.elections}}、{{aitem.value}}</text> 
						<image src="@/static/img/select@2x.png" v-if="aitem.checked == false"></image>  
						<image src="@/static/img/selected@2x.png" v-else></image>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {getUrlParam} from '@/static/js/filter.js'
	export default{
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				start:'',
				end:currentDate,
				start_date:'',
				readType:'',
				questionData:{},
				rapidScreen:{},
				selected:''
			}
		},
		created() {
			const _this = this;
			_this.selected = getUrlParam('selected');
			_this.question().then((reponse) => {
				if(getUrlParam('id')) {
					const _this = this;
					_this.id = getUrlParam('id');		
					_this.$api.psychologyQuestionInfo(_this.id).then((res) => {
						if(res.data.returnCode == 200) {
							const data1 = res.data.result && res.data.result.newUserTopicsEntities;
							const data2 = res.data.result && res.data.result.newAppMentalHealthAssessmentEntity;
							_this.userId = data2.userId;
							_this.healthId = data2.healthId;
							_this.start_date = data2.fillFormDate;
							_this.editTid = data2.tid
							_this.questionData[1].adTopicsRepliesEntityList.map((a)=>{
								data1.map((c) => {
									if(a.id == c.titleId) {
										a.adTopicsRepliesEntityList.map((d) => {
											d.nid = c.id;
											if(d.elections == c.userOption) {
												d.checked = true;
											}
										})
									}
								})
							})
							_this.rapidScreen = _this.questionData[0].adTopicsRepliesEntityList.map((a)=>{
								data1.map((c) => {
									if(a.id == c.titleId) {
										a.adTopicsRepliesEntityList.map((d) => {
											if(d.elections == c.userOption) {
												a.setvalue = c.userValue;
												a.elections = c.userOption
												a.nid = c.id
											}
										})
									}
								})
								return a
							})
						}
					})
				}
			});
			this.rapidScreen = localStorage.getItem('rapidScreen') && JSON.parse(localStorage.getItem('rapidScreen'))
		},
		methods:{
			showModalbtn(e) {
				if(!this.start_date) {
					uni.showToast({
						title:'请选择日期',
						icon: 'none'
					})
					return 
				}
				
				//过滤选中的值
				let appUserTopicsEntities = []
				this.questionData[0] && this.questionData[0].adTopicsRepliesEntityList.map((a,ai) => {
					appUserTopicsEntities.push({
						titleId: a.id,
						id: a.nid,
						userOption:this.rapidScreen[ai].elections && this.rapidScreen[ai].elections ||  a.elections,
						userValue: this.rapidScreen[ai].setvalue && this.rapidScreen[ai].setvalue || a.setvalue
					})
				})
				
				this.questionData[1] && this.questionData[1].adTopicsRepliesEntityList.map((a,ai) => {
					let bresult = a.adTopicsRepliesEntityList.filter((b) => {
						return b.checked
					})
					appUserTopicsEntities.push({
						titleId: a.id,
						id: bresult.length > 0 && bresult[0].nid || a.adTopicsRepliesEntityList[0].nid,
						userOption: bresult.length > 0 ? bresult[0].elections : '',
						userValue: bresult.length > 0 ? bresult[0].value : ''
					})
					
				})
				
				const record = {
					appUserTopicsEntities: appUserTopicsEntities,
					appUserTopicsTitleEntity: {
						healthId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).healthId ||'',
						userId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId || '',
					},
					fillFormDate: this.start_date,
					source:1,
					status: e,
					tid: [27,29],
					upload:6,
					userId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId || '',
					healthId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).healthId ||'',
				}
				
				localStorage.removeItem('rapidScreen')

				if(this.id) {
					record.id = Number(this.id);
					record.appUserTopicsTitleEntity.id = this.editTid
					this.$api.psychologyQuestionUpdate(record).then((res) => {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/healthStatus/navigation?current=5'
							})
						},2000)
					})
				}else {
					this.$api.psychologyQuestionAdd(record).then((res) => {
						if(res.data.returnCode == 200) {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/healthStatus/navigation?current=5'
								})
							},2000)
						}
					})
				}
				
			},
			//选答案
			answer(item,aitem) {
				if(this.selected) {
					return
				}
				item.adTopicsRepliesEntityList.map((a) => {
					a.checked = false
					return a
				})
				aitem.checked = true;
			},
			//获取焦虑问卷
			question() {
				return new Promise((success,error)=>{
					this.$api.psychologyQuestion('ids=27&ids=29').then((res)=>{
						if(res){
							const data = Object.keys(res.data.result).map((a) => {
								const aData = res.data.result[a].adTopicsRepliesEntityList.map((b) => {
									b.adTopicsRepliesEntityList.map((c) => {
										c.checked = false;
										return b
									})
									return b
								})
								return res.data.result[a]
							})
							this.questionData = data;
							success(res.data.result)
						}else{
							error('')
						}
					})
				})
			},
			// 日期
			bindDateChange(e) { 
				this.start_date = e.target.value;
			},
			// 获取当前时间
			getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/monitor/accomplishment/index.scss';
</style>
